package com.youku.adSDK.view.components
{
   import flash.display.Sprite;
   import com.youku.adSDK.model.vo.ADData;
   import flash.display.Loader;
   import flash.display.SimpleButton;
   import flash.display.MovieClip;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.ADEventDispather;
   import flash.net.URLRequest;
   import com.youku.utils.AssetUtil;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.MouseEvent;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.view.events.FlashADEvent;
   import com.youku.adSDK.Utils.AdOpenWindow;
   import com.youku.adSDK.YoukuAdEvent;
   import flash.net.sendToURL;
   
   public class PauseAD extends Sprite
   {
      
      public function PauseAD() {
         super();
      }
      
      private var _data:ADData;
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _mask:Sprite;
      
      private var _content:Sprite;
      
      private var _loader:Loader;
      
      private var _clickSp:Sprite;
      
      private var _closer:SimpleButton;
      
      private var _resumeCloser:MovieClip;
      
      private var _fullPause:Boolean;
      
      private var _sw:Number;
      
      private var _sh:Number;
      
      private var _barH:Number;
      
      private var _context:ADContext;
      
      private var _eventDispather:ADEventDispather;
      
      public function init(param1:ADData, param2:Number, param3:Number, param4:Boolean, param5:Number, param6:Number, param7:Number, param8:ADContext, param9:ADEventDispather) : void {
         this._data = param1;
         this._width = param2;
         this._height = param3;
         this._fullPause = param4;
         this._sw = param5;
         this._sh = param6;
         this._barH = param7;
         this._context = param8;
         this._eventDispather = param9;
         if(param1.RS != "miaozhentest.swf")
         {
            this.createItems();
            this.addEvents();
         }
      }
      
      private function createItems() : void {
         this.sendData(this._data.SUS);
         this._content = new Sprite();
         this.addChild(this._content);
         this._mask = new Sprite();
         this.drawRect(this._mask,1);
         this.addChild(this._mask);
         this._content.mask = this._mask;
         this._loader = new Loader();
         this._content.addChild(this._loader);
         this._loader.load(new URLRequest(this._data.RS));
         this._clickSp = new Sprite();
         this._content.addChild(this._clickSp);
         this.drawRect(this._clickSp,0);
         this._clickSp.buttonMode = true;
         if(this._fullPause)
         {
            this._resumeCloser = AssetUtil.getAsset("AD_resumeCloser") as MovieClip;
            this.addChild(this._resumeCloser);
            this._resumeCloser.stop();
            this._resumeCloser.visible = false;
         }
         else
         {
            this._closer = AssetUtil.getAsset("AD_closer") as SimpleButton;
            this.addChild(this._closer);
            this._closer.visible = false;
         }
         if(Number(this._data.CF) == 2 || Number(this._data.CF) == 3)
         {
            this._clickSp.visible = false;
         }
         this.layoutItems();
      }
      
      private function createMiaozhenItems() : void {
         this.sendData(this._data.SUS);
         this._content = new Sprite();
         this.addChild(this._content);
         this._mask = new Sprite();
         this.drawRect(this._mask,1);
         this.addChild(this._mask);
         this._content.mask = this._mask;
         this._clickSp = new Sprite();
         if(this._fullPause)
         {
            this._resumeCloser = AssetUtil.getAsset("AD_resumeCloser") as MovieClip;
            this.addChild(this._resumeCloser);
            this._resumeCloser.stop();
            this._resumeCloser.visible = false;
         }
         else
         {
            this._closer = AssetUtil.getAsset("AD_closer") as SimpleButton;
            this.addChild(this._closer);
            this._closer.visible = false;
         }
         this.layoutItems();
      }
      
      private function addEvents() : void {
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoaderComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoaderError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onLoaderError);
         this._clickSp.addEventListener(MouseEvent.CLICK,this.onClickSpClick);
         if(this._fullPause)
         {
            this._resumeCloser.playVideo.addEventListener(MouseEvent.CLICK,this.onPlayVideoClick);
            this._resumeCloser.closeAd.addEventListener(MouseEvent.CLICK,this.onCloseAdClick);
         }
         else
         {
            this._closer.addEventListener(MouseEvent.CLICK,this.onCloserClick);
         }
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerReset);
         if(this._data.CF == 2)
         {
            this._loader.addEventListener(MouseEvent.CLICK,this.onADSelfClick,true);
         }
      }
      
      private function onADSelfClick(param1:MouseEvent) : void {
         this.sendData(this._data.CUM);
      }
      
      private function onLoaderComplete(param1:Event) : void {
         this.dispatchEvent(new FlashADEvent(FlashADEvent.CONTENT_LOADED));
         if(this._fullPause)
         {
            this._resumeCloser.visible = true;
         }
         else
         {
            this._closer.visible = true;
         }
      }
      
      private function onLoaderError(param1:Event) : void {
         this.dispatchEvent(new FlashADEvent(FlashADEvent.CONTENT_LOAD_ERROR));
      }
      
      private function onClickSpClick(param1:MouseEvent) : void {
         this.sendData(this._data.CUM);
         if(!(this._data.CU == null) && !(this._data.CU == ""))
         {
            AdOpenWindow.navigateOpen(new URLRequest(this._data.CU),"_blank");
         }
      }
      
      private function onCloserClick(param1:MouseEvent) : void {
         this.dispose();
      }
      
      private function onPlayVideoClick(param1:MouseEvent) : void {
         this.dispose();
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_PAUSE_PLAY_BUTTON_CLICK));
      }
      
      private function onCloseAdClick(param1:MouseEvent) : void {
         this.dispose();
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._sw = param1.data.width;
         this._sh = param1.data.height;
         this._barH = param1.data.cheight;
         this.layoutItems();
      }
      
      private function onPlayerReset(param1:ADEvent) : void {
         this.dispose();
      }
      
      private function onMiaozhenLoaded(param1:ADEvent) : void {
         if(this._fullPause)
         {
            this._resumeCloser.visible = true;
         }
         else
         {
            this._closer.visible = true;
         }
         this._width = param1.data.width;
         this._height = param1.data.height;
         this.drawRect(this._mask,1);
         this.layoutItems();
      }
      
      private function onMiaozhenLoadError(param1:ADEvent) : void {
         this.dispatchEvent(new FlashADEvent(FlashADEvent.CONTENT_LOAD_ERROR));
      }
      
      private function layoutItems() : void {
         if(!stage)
         {
            return;
         }
         var _loc1_:Number = this._sw;
         var _loc2_:Number = this._sh - this._barH;
         if(this.hasSearchBar)
         {
            _loc2_ = _loc2_ - 72;
         }
         var _loc3_:Number = 1;
         if(this._width / this._height > _loc1_ / _loc2_)
         {
            if(this._width > _loc1_)
            {
               _loc3_ = _loc1_ / this._width;
            }
         }
         else if(this._height > _loc2_)
         {
            _loc3_ = _loc2_ / this._height;
         }
         
         this._content.scaleX = this._content.scaleY = _loc3_;
         this._mask.scaleX = this._mask.scaleY = _loc3_;
         this._content.x = this._mask.x = (_loc1_ - this._mask.width) / 2;
         this._content.y = this._mask.y = (this._sh - this._barH - this._height * _loc3_) / 2;
         if(this._fullPause)
         {
            this._resumeCloser.x = this._content.x + this._mask.width - this._resumeCloser.width;
            this._resumeCloser.y = this._content.y;
         }
         else
         {
            this._closer.x = this._content.x + this._mask.width - this._closer.width;
            this._closer.y = this._content.y;
         }
      }
      
      private function drawRect(param1:Sprite, param2:Number) : void {
         param1.graphics.clear();
         param1.graphics.beginFill(0,param2);
         param1.graphics.drawRect(0,0,this._width,this._height);
         param1.graphics.endFill();
      }
      
      private function sendData(param1:Array) : void {
         var _loc2_:* = 0;
         var _loc3_:Object = null;
         var _loc4_:String = null;
         if((param1) && param1.length > 0)
         {
            _loc2_ = 0;
            while(_loc2_ < param1.length)
            {
               _loc3_ = param1[_loc2_] as Object;
               if((_loc3_) && (_loc3_.hasOwnProperty("U")))
               {
                  _loc4_ = param1[_loc2_].U as String;
                  if((_loc4_) && !(_loc4_.indexOf("http://") == -1))
                  {
                     sendToURL(new URLRequest(_loc4_));
                  }
               }
               _loc2_++;
            }
         }
      }
      
      private function get hasSearchBar() : Boolean {
         if(this._context.winType == "exterior" || this._context.winType == "BDskin")
         {
            if(this._context.isInFullScreen)
            {
               return false;
            }
            return true;
         }
         return false;
      }
      
      public function dispose() : void {
         if((this._mask) && (this._mask.parent))
         {
            this._mask.graphics.clear();
            this._clickSp.graphics.clear();
            this.removeChild(this._mask);
            this.removeChild(this._content);
            if(this._fullPause)
            {
               this.removeChild(this._resumeCloser);
            }
            else
            {
               this.removeChild(this._closer);
            }
         }
      }
   }
}
